<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="内容类型名" prop="contentTypeName">
      <el-input v-model="dataForm.contentTypeName" placeholder="内容类型名"></el-input>
    </el-form-item>
    <el-form-item label="内容类型英文名" prop="contentTypeNameEn">
      <el-input v-model="dataForm.contentTypeNameEn" placeholder="内容类型英文名"></el-input>
    </el-form-item>
    <el-form-item label="内容类型logo" prop="contentTypeLogo">
      <el-input v-model="dataForm.contentTypeLogo" placeholder="内容类型logo"></el-input>
    </el-form-item>
    <el-form-item label="内容类型图片地址" prop="contentTypeUrl">
      <el-input v-model="dataForm.contentTypeUrl" placeholder="内容类型图片地址"></el-input>
    </el-form-item>
    <el-form-item label="是否删除 0：存在 1：已删除" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="是否删除 0：存在 1：已删除"></el-input>
    </el-form-item>
    <el-form-item label="状态 0：可用 1：不可用" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态 0：可用 1：不可用"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder="创建者"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          contentTypeId: 0,
          contentTypeName: '',
          contentTypeNameEn: '',
          contentTypeLogo: '',
          contentTypeUrl: '',
          delFlag: '',
          status: '',
          createTime: '',
          updateTime: '',
          createUser: ''
        },
        dataRule: {
          contentTypeName: [
            { required: true, message: '内容类型名不能为空', trigger: 'blur' }
          ],
          contentTypeNameEn: [
            { required: true, message: '内容类型英文名不能为空', trigger: 'blur' }
          ],
          contentTypeLogo: [
            { required: true, message: '内容类型logo不能为空', trigger: 'blur' }
          ],
          contentTypeUrl: [
            { required: true, message: '内容类型图片地址不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '是否删除 0：存在 1：已删除不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态 0：可用 1：不可用不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.contentTypeId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.contentTypeId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webcontenttype/info/${this.dataForm.contentTypeId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.contentTypeName = data.webContentType.contentTypeName
                this.dataForm.contentTypeNameEn = data.webContentType.contentTypeNameEn
                this.dataForm.contentTypeLogo = data.webContentType.contentTypeLogo
                this.dataForm.contentTypeUrl = data.webContentType.contentTypeUrl
                this.dataForm.delFlag = data.webContentType.delFlag
                this.dataForm.status = data.webContentType.status
                this.dataForm.createTime = data.webContentType.createTime
                this.dataForm.updateTime = data.webContentType.updateTime
                this.dataForm.createUser = data.webContentType.createUser
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webcontenttype/${!this.dataForm.contentTypeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'contentTypeId': this.dataForm.contentTypeId || undefined,
                'contentTypeName': this.dataForm.contentTypeName,
                'contentTypeNameEn': this.dataForm.contentTypeNameEn,
                'contentTypeLogo': this.dataForm.contentTypeLogo,
                'contentTypeUrl': this.dataForm.contentTypeUrl,
                'delFlag': this.dataForm.delFlag,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'createUser': this.dataForm.createUser
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
